using System.Collections;
using System.Collections.Generic;
using UnityEngine;

enum SortMode {
	Self,
	Parent
}

[RequireComponent(typeof(SpriteRenderer))]
public class SortController:MonoBehaviour {

	void Start() {
		if(transform.parent) parent=transform.parent.GetComponent<SortController>();
		sprite=GetComponent<SpriteRenderer>();
	}

	void Update() {
		if(sortMode==SortMode.Parent&&parent) {
			sortPointY=parent.sortPointY+offsetY;
			sortOrder=-Mathf.FloorToInt(sortPointY*32);
		} else {
			sortPointY=transform.position.y+offsetY;
			sortOrder=-Mathf.FloorToInt(sortPointY*32);
		}

		sprite.sortingOrder=sortOrder;
	}

	[SerializeField] SortMode sortMode;
	[SerializeField] float offsetY;

	SpriteRenderer sprite;
	SortController parent;
	float sortPointY;
	int sortOrder;
}